//
//  CLOtherAlgorithm.m
//  Charles
//
//  Created by Charles on 2019/7/23.
//  Copyright © 2019 charles. All rights reserved.
//

#import "CLOtherAlgorithm.h"

@implementation CLOtherAlgorithm

+ (NSInteger)numberStringToInteger:(NSString *)numberString {
    if (numberString.length == 0) {
        return 0;
    }
    NSInteger length = numberString.length;
    NSInteger resultNumber = 0;
    const char *numberArray = [numberString UTF8String];
    
    for (NSUInteger index = 0; index < length; index++) {
        char number = numberArray[index];
        if (number > '0' && number < '9') {
            int tempNumber = number - '0'; // 数字字符减去字符'0' 得出的就是对应的十进制值，其实是ASCII码值的差。
            NSLog(@"%d",number - '0');
            // pow函数是求一个数的n次方，这里是10的 length - index - 1 次方。
            resultNumber += pow(10,length - index - 1) * tempNumber;
        }else{
            NSLog(@"invalid number string!");
            resultNumber = 0;
            break;
        }
    }
    return resultNumber;
}

@end
